package com.csxy.hql.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.hql.R;
import com.csxy.hql.entity.EduTeacher;
import com.csxy.hql.entity.vo.TeacherQuery;
import com.csxy.hql.service.EduTeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "edu-teacher",description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")

public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
    @Operation(summary = "查询所有讲师")
    @GetMapping("findAll")
    public R getAllTeacher(){
        List<EduTeacher> list = eduTeacherService.list();
        return R.ok().data("items",list);
    }

    @Operation(summary = "逻辑删除讲师")
    @DeleteMapping("/deleteById/{id}")
    public R deleteTeacher(@PathVariable("id") String id){
        boolean result = eduTeacherService.removeById(id);
        if(result){
            return R.ok();
        }else{
            return R.error();
        }
    }


    @Operation(summary = "分页查询讲师")
    @GetMapping("/pageList/{page}/{limit}")
    public R pageListTeacher(@PathVariable("page") long page,
                             @PathVariable("limit") long limit) {
        Page<EduTeacher> pagelist = eduTeacherService.pageListTeacher(page, limit);
        return R.ok().data("total", pagelist.getTotal()).data("rows", pagelist.getRecords());
    }

    // 新增讲师
    @Operation(summary = "新增讲师",description = "新增讲师")
    @PostMapping("/save")
    public R save(@RequestBody EduTeacher eduTeacher){
        boolean flag = eduTeacherService.save(eduTeacher);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }


    //根据id查询，用于信息回显
    @Operation(summary = "根据id查询",description = "根据id查询")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable("id") String id){
        EduTeacher teacher = eduTeacherService.getById(id);
        return R.ok().data("item",teacher);
    }

    //修改讲师
    @Operation(summary = "修改讲师",description = "修改讲师")
    @PostMapping("/updateById")
    public R updateById(@RequestBody EduTeacher teacher){
        boolean flag = eduTeacherService.updateById(teacher);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    //条件查询讲师带分页
    @Operation(summary = "条件查询讲师带分页",description = "条件查询讲师带分页")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public R pageListTeacherCondition(@Parameter(name = "page",description = "当前页码",required = true)
                                      @PathVariable long page,
                                      @Parameter(name = "limit",description = "每页记录数",required = true)
                                      @PathVariable long limit,
                                      @RequestBody(required = false) TeacherQuery teacherQuery){ //通过封装TeacherQuery对象，封装查询条件
        // 创建分页page对象
        Page<EduTeacher> pageParm = new Page<>(page,limit);
        //调用方法实现条件查询分页
        eduTeacherService.PageQuery(pageParm,teacherQuery);
        // 获取查询到的结果数据
        List<EduTeacher> records = pageParm.getRecords();
        // 获取总记录数
        long total = pageParm.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

}
